package com.df.springmvc.system.log.dao;

import java.util.ArrayList;
import java.util.HashMap;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.common.bean.SQLInsertBean;
import com.df.springmvc.supper.SupperDao;

@Repository
public class LogDao extends SupperDao{
	private String message;
	
	/**
	 * *
	 * @param sql
	 *            查询的SQL
	 * @param param_HashMap
	 *            查询参数
	 * @param i
	 *            从第几天开始
	 * @param j
	 *            显示多少条数据
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Object> query(HashMap<String,String> map,Object[] attribute,int i,int j) throws Exception{
		// TODO Auto-generated method stub
		Session session=getSessionFactory().getCurrentSession();
		String sql="select LOGID,MODUAL,CONTENT,USERNAME,b.staffName,date_format(o.CREATETIME,'%Y-%m-%d %l:%m:%s') as CREATETIME from s_log_info o left join b_staff_info b on  b.userAccount=o.USERNAME  where 1=1 ";
		if((map.get("staffName") != null && !"".equals(map.get("staffName")))){
			sql=sql +" and b.staffName LIKE :s ";
		}
		if((map.get("startTime") != null && !"".equals(map.get("startTime")))||(map.get("endTime") != null && !"".equals(map.get("endTime")))){
			sql=sql +" and o.createTime>=:startTime ' 00:00:00'  ";
			sql=sql +" and  o.createTime<=:endTime ' 23:59:59' ";
		}
		sql = sql+" order by o.createTime desc";
		SQLQuery query=session.createSQLQuery(sql);
		if((map.get("staffName") != null && !"".equals(map.get("staffName")))){
			query.setParameter("s","%" + map.get("staffName") + "%");
		}
		if((map.get("startTime") != null && !"".equals(map.get("startTime")))||(map.get("endTime") != null && !"".equals(map.get("endTime")))){
			query.setParameter("startTime", map.get("startTime") );
			query.setParameter("endTime", map.get("endTime"));
		}
		query.setFirstResult(i);
		query.setMaxResults(j);
		return (ArrayList<Object>)query.list();
	}


	/**
	 * 单条数据查询
	 * @param map
	 * @return
	 */
	public Object query(HashMap<String,String> map){
		// TODO Auto-generated method stub
		Session session=getSessionFactory().getCurrentSession();
		String sql="select * from s_org_info where 1=1 and STATUS = 1 and guid=:g";
		SQLQuery query=session.createSQLQuery(sql);
		query.setString("g",map.get("guid"));
		return query.list();
	}
	/**
	 * 数据添加
	 */
	@Override
	public Object add(HashMap<String,Object> map) throws Exception{
		// TODO Auto-generated method stub
		SQLInsertBean insertBean=new SQLInsertBean("s_log_info");
		HashMap<String,Object> columnMap=new HashMap<String,Object>(map);
		insertBean.setColumnMap(columnMap);
		Object o=excuteSqlBean(insertBean);
		return o;
	}

	
	/**
	 * 查看数据
	 */
	@Override
	public HashMap<String,Object> view(String guid) throws Exception{
		// TODO Auto-generated method stub
		String sql="select guid,'',createby,createtime,updateby,updatetime,orgCode,orgName,pCode,description,orgTypeCode,(select expTabName from bm_business_info where status ='1' and expTabCode =orgTypeCode) as 'orgType' from s_org_info where 1=1 and status =1 and guid=:guid";
		HashMap<String,Object> tmp=new HashMap<String,Object>();
		tmp.put("guid",guid);
		ArrayList<HashMap<String,Object>> list=querySqlToSQLResultBean(sql,tmp,0,0).getMapList();
		if(list.size() == 1){
			return list.get(0);
		}else{
			throw new Exception("找不到数据");
		}
	}

	public boolean check(String guid) throws Exception {
		// TODO Auto-generated method stub
		String sql="select * from s_org_info where guid=:guid";
		HashMap<String,Object> tmp=new HashMap<String,Object>();
		tmp.put("guid",guid);
		ArrayList<HashMap<String,Object>> list=querySqlToSQLResultBean(sql,tmp,0,0).getMapList();
		if(list.size() == 0){
			return true;
		}else{
			return false;
		}
	}

	public String getMessage(){
		return message;
	}
	public void setMessage(String message){
		this.message=message;
	}
	
	
	
	
	
}
